<?php

class TransferController extends AController
{


	public function actionIndex()
	{
		$condition = '';
		if(isset($_GET['search']))
		{
			//print_r($_GET);
			$condition = " 1=1 ";
			if($_GET['search']['s_username'])
			{
				$condition .= " AND transfer_c_usernames LIKE '%".urldecode($_GET['search']['s_username'])."%' ";
			} 
			if($_GET['search']['s_status'])
			{
				$condition .= "AND status = '".$_GET['search']['s_status']."' ";
			}
			if($_GET['search']['s_o_d'])
			{
				$condition .= "AND from_department_id = '".$_GET['search']['s_o_d']."' ";
			}
			if($_GET['search']['s_n_d'])
			{
				$condition .= "AND to_department_id = '".$_GET['search']['s_n_d']."' ";
			}
			if($_GET['search']['s_t_start'] && $_GET['search']['s_t_end'])
			{
				$condition .= "AND effective_time <= '".strtotime($_GET['search']['s_t_end'])."' AND effective_time >=  '".strtotime($_GET['search']['s_t_start'])."'" ;
			}
		}
		//echo $condition;
		$serch = isset($_GET['search'])?$_GET['search']:array();

		$pageSize = ($_GET['search']['type'] == 'csv') ? 10000 : 30;

		$dataProvider = new CActiveDataProvider('Transfer',array(
			'criteria'=>array(
				'order' => 'id DESC',
				'condition' => $condition
				),
			'pagination'=>array(
		        'pageSize'=>$pageSize,
		    	),
			)
		);
		if($_GET['search']['type'] == 'csv')
		{
			$arr = $dataProvider->getData() ;
			foreach ($arr as $key => $value)
			{
				$arr[$key]['effective_time'] = date("Y-m-d H:i" , $value['effective_time']);
				$arr[$key]['create_time'] = date("Y-m-d H:i" , $value['create_time']);
				if($value['user'])
				{
					$arr[$key]['category'] = '';
					foreach ($value['user'] as $k => $v) 
					{
						if($v['new_attr_id'] || $v['new_L'] || $v['new_M']) 
						{
							$arr[$key]['category'] .= $v['c_username'] .":";
							$arr[$key]['category'] .= "ATTR : ".HrPositionAttribute::model()->getInfoById($v->new_attr_id,'title') ."-";
							$arr[$key]['category'] .= "L : ".HrPositionLevel::model()->getInfoById($v->new_L,'name')."-";
							$arr[$key]['category'] .= "M : ".HrPositionLevel::model()->getInfoById($v->new_M,'name')."|";
						}
					}
				}
			}
			Csv::exportCsv(Yii::app()->params['transferFields'],$arr);
			Yii::app()->end();
		}
		//部门
		$ud = UserDepartment::model()->findAllByAttributes(array('status'=>1));
		$this->render('index',array(
			'dataProvider'=>$dataProvider,'ud'=>$ud,'statusName'=>Tinit::statusName(),'search'=>$serch,
		));
	}

	public function actionDetail()
	{
		$model = new Transfer;
		$data = $model->getTransferDetails((int)$_GET['id']);
		if(!$model->detailCheck($data,$this->userInfo->unique_id))
		{

		}
		//print_r($data);
		$this->render('detail',array(
			'data'=>$data,
		));
	}

	public function actionEdit($id)
	{
		$model = new Transfer;
		$statusOption = Tinit::statusName();

		$data = $model->getTransferDetails((int)$_GET['id']);

		if($_POST)
		{

			//print_r($_POST);
			$model=$model->findByPk($_POST['id']);
			$model->status = $_POST['status'];
			if($model->update())
			{
				//echo '!!!';
				//保存transfer index
				$transferIndexModel = new TransferIndex;
				$transferIndexModel->transfer_id = $model->id;
				$transferIndexModel->action_unique_id = $this->userInfo->unique_id;
				$transferIndexModel->action_type = -1;
				//$transferIndexModel->t_index = 0;
				$transferIndexModel->dateline = $_SERVER['REQUEST_TIME'];
				$transferIndexModel->save();
				//发送邮件
				//TSendMail::send($this->userInfo , $model);
				//die;
				$this->showAlert(array('type'=>'1','content'=>'更新成功！','url'=>'admincp/transfer/edit/id/'.$model->id));
			} 
			else 
			{
				die;
			}
		}

		$this->render('edit',array(
			'data'=>$data,'statusOption'=>$statusOption,
		));
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}